package com.develop.mybluetooth.adapter

import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattService
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.develop.mybluetooth.R
import com.develop.mybluetooth.databinding.ItemBleCharacteristicsBinding
import com.develop.mybluetooth.databinding.ItemBleServiceBinding


class CharacteristicsAdapter : BaseQuickAdapter<BluetoothGattCharacteristic, CharacteristicsAdapter.VH>() {

    class VH(
        parent: ViewGroup,
        val binding: ItemBleCharacteristicsBinding = ItemBleCharacteristicsBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)
    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {

        return VH(parent)
    }


    override fun onBindViewHolder(holder: VH, position: Int, item: BluetoothGattCharacteristic?) {
        holder.binding.tvChTitle.text = context.getString(R.string.s_service) + "（"+position + ")"
        holder.binding.tvChUuid.text = item?.uuid.toString()

        val charaProp: Int = item?.properties ?:0
        val sb = StringBuilder()
        if ((charaProp and BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            sb.append("Read")
        }
        if ((charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
            sb.append(",")
            sb.append("Write")
        }
        if ((charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
            sb.append(",")
            sb.append("Write No Response")
        }
        if ((charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            sb.append(",")
            sb.append("Notify")
        }
        if ((charaProp and BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
            sb.append(",")
            sb.append("Indicate")
        }
        sb.toString().startsWith(",")
        holder.binding.tvStatus.text = sb.toString()
    }

}